
import java.io.Serializable;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;

public class TripleDES extends Message implements Serializable {

    private String DESKey;
    private String DESKey2;

    public TripleDES(User u, int friend, String mText) {
        super(u, friend, mText);
    }

    public boolean setDESKey(String key) {
        if (key.length() >= 8) {
            DESKey = key;
            return true;
        }
        return false;
    }

    public boolean setDESKey2(String key) {
        if (key.length() >= 8) {
            DESKey2 = key;
            return true;
        }
        return false;
    }

    public boolean setDESKey() {
        if (super.getUser() != null && super.getFriendID() != 0) {
            DESKey = super.getUser().findFriend(super.getFriendID()).
                    getDESKey();
            return true;
        }
        return false;
    }

    public boolean setDESKey2() {
        if (super.getUser() != null && super.getFriendID() != 0) {
            DESKey2 = super.getUser().findFriend(super.getFriendID()).
                    getDESKey2();
            return true;
        }
        return false;
    }

    public boolean encrypt() {
        try {
            DESKeySpec dks = new DESKeySpec(DESKey.getBytes());
            DESKeySpec dks2 = new DESKeySpec(DESKey2.getBytes());
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey desKey = skf.generateSecret(dks);
            SecretKey desKey2 = skf.generateSecret(dks2);
            Cipher cipher = Cipher.getInstance("DES");

            byte[] output = text.getBytes("UTF8");
            cipher.init(Cipher.ENCRYPT_MODE, desKey);
            output = cipher.doFinal(output);
            cipher.init(Cipher.DECRYPT_MODE, desKey2);
            output = cipher.doFinal(output);
            cipher.init(Cipher.ENCRYPT_MODE, desKey);
            output = cipher.doFinal(output);
            text = new sun.misc.BASE64Encoder().encode(output);
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    public boolean decrypt() {
        try {
            DESKeySpec dks = new DESKeySpec(DESKey.getBytes());
            DESKeySpec dks2 = new DESKeySpec(DESKey2.getBytes());
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey desKey = skf.generateSecret(dks);
            SecretKey desKey2 = skf.generateSecret(dks2);
            Cipher cipher = Cipher.getInstance("DES");


            byte[] output = new sun.misc.BASE64Decoder().decodeBuffer(text);
            cipher.init(Cipher.DECRYPT_MODE, desKey);
            output = cipher.doFinal(output);
            cipher.init(Cipher.ENCRYPT_MODE, desKey2);
            output = cipher.doFinal(output);
            cipher.init(Cipher.DECRYPT_MODE, desKey);
            output = cipher.doFinal(output);
            text = new String(output, "UTF8");
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    public static String generateKey() {
        int size = 8;
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0"
                + "123456789";
        String ret = "";
        int length = chars.length();
        for (int i = 0; i < size; i++) {
            ret += chars.split("")[ (int) (Math.random() * (length - 1))];
        }
        return ret;
    }
}